import { Component, Input, OnInit, Host, Inject, forwardRef } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { TopicInfo, RowInfo } from '../../models/quesinfo';
/**
 * Generated class for the QuestionSinglePage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */


@Component({
  selector: 'page-question-single',
  templateUrl: 'question-single.html',
})
export class QuestionSinglePage implements OnInit {
  @Input() testData;
  @Input() topic: TopicInfo;@Input() poneSet: Array<any>;
  @Input() topicIndex;
  topicIndexshow;

  thisFQId;
  //

  constructor(public navCtrl: NavController, public navParams: NavParams) {

  }
  pickOption(a, b, c, d) {
    this.pickOption1(a, b, c, d);
  };
  //选择选项时间，逻辑时间
  pickOption1(topic, topicid, optionid, optionContent) {
    let listTopic = this.testData.Que.listTopic;
    const topictype = topic.FType  //
    const topicsortsort = parseInt(topic.FOrderNum);
    if (topic.FGotoTopic != "" && topic.FGotoTopic != null) {
      //无条件跳转
      this.wtjtz(topic);
    }
    else {
      //逻辑跳转
      if ((topictype == "10001") || (topictype == "10012")) {

        // 根据选项判断
        // 首先显示全部

        // 隐藏相关项目

        let thislistRows = new Array<RowInfo>();
        thislistRows = topic.listRow;
        let thisTempResult = topic.TempResult;
        if (thislistRows.filter(p => p.FROptionId == thisTempResult)) {
          let thisOption = thislistRows.filter(p => p.FROptionId == thisTempResult)[0];
          let istzt = false;
          let skipOptionid = "";
          let skipTopic = "";
          thislistRows.forEach((onerow, index) => {
            //判断逻辑跳转题
            if (onerow.FSkip && optionid === onerow.FROptionId) {
              skipTopic = onerow.FSkip;
              skipOptionid = onerow.FROptionId;
              istzt = true;
            }
          });
          console.log(this.topic.listRow);
          console.log(this.testData.Que.listTopic);
          this.testData.Que.listTopic.forEach((onetopic, index) => {
            const findTopic = this.topic.listRow.filter(topicRow => topicRow.FSkip === onetopic.FTopicId).length;
            console.log(findTopic);
            if (findTopic) {
              onetopic.FShow = true;
            }
          });
          if (istzt) {

            let gotoTopic = listTopic.filter(p => p.FTopicId == skipTopic)[0];
            if (gotoTopic) {
              let gotoIndex = gotoTopic.FOrderNum;
              if (skipOptionid == topic.TempResult) {
                this.TopicNone(topicsortsort, gotoIndex);
              }
              else {
                this.TopicDisplay(topicsortsort, gotoIndex);
              }
            }
          }
        }
      }
    }
    //隐藏
    if ((topictype == "10001") || (topictype == "10012")) {
      //判断隐藏题
      let isyct = false;
      let thislistRows = new Array<RowInfo>();
      thislistRows = topic.listRow;

      thislistRows.forEach((onerow, indexo) => {
        if (onerow.FROptionId == topic.TempResult) {
          listTopic.forEach((onetopic, indext) => {
            let fsort = onetopic.FOrderNum;
            if (onetopic.FycOpt != null && onetopic.FycOpt != "" && onetopic.FycOpt != "null" && onetopic.FycOpt != undefined) {
              if (onetopic.FycOpt == onerow.FROptionId) {
                onetopic.FShow = true;
              }
            }
          })
        }
        else {
          listTopic.forEach((onetopic, indext) => {
            let fsort = onetopic.FOrderNum;
            if (onetopic.FycOpt != null && onetopic.FycOpt != "" && onetopic.FycOpt != "null" && onetopic.FycOpt != undefined) {
              if (onetopic.FycOpt == onerow.FROptionId) {
                onetopic.FShow = false;
              }
            }
          })
        }
      })
    }
    //题目引用
    listTopic.forEach((onetopic, index) => {
      if (onetopic.FQuoteTopic == topic.FTopicId) {
        onetopic.FTitle = onetopic.FTitleYuan.replace("[-引用位置-]", optionContent);
      }
    })
  }
  //逻辑跳转
  wtjtz(topic) {
    let listTopic = this.testData.Que.listTopic;
    const topictype = topic.FType  //
    const topicsortsort = parseInt(topic.FOrderNum);
    if (topic.FGotoTopic != "" && topic.FGotoTopic != null) {
      //无条件跳转
      if (listTopic.filter(p => p.FTopicId == topic.FGotoTopic) != undefined && listTopic.filter(p => p.FTopicId == topic.FGotoTopic) != null) {
        let gotoTopic = listTopic.filter(p => p.FTopicId == topic.FGotoTopic)[0];
        let gotoIndex = gotoTopic.FOrderNum;
        this.TopicNone(topicsortsort, gotoIndex);
      }
    }
  }
  //
  TopicNone(startsort, endsort) {
    let listTopic = this.testData.Que.listTopic;
    listTopic.forEach((onetopic, index) => {
      let fsort = onetopic.FOrderNum;
      if (fsort > startsort && fsort < endsort) {
        onetopic.FShow = false;
      }
    })
  }
  //
  TopicDisplay(startsort, endsort) {
    let listTopic = this.testData.Que.listTopic;
    listTopic.forEach((onetopic, index) => {
      let fsort = onetopic.FOrderNum;
      if (fsort > startsort && fsort < endsort) {
        onetopic.FShow = true;
      }
    })
  }
  ngOnInit(): void {
    this.topicIndexshow = this.topicIndex + 1;
    this.thisFQId = this.topic.FQId;
  }

  ionViewDidLoad() {
    //console.log('ionViewDidLoad QuestionSinglePage');
  }

}
